/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.winner.lps.modules.app.web;

import com.winner.lps.common.config.Global;
import com.winner.lps.common.persistence.Page;
import com.winner.lps.common.utils.StringUtils;
import com.winner.lps.common.web.BaseController;
import com.winner.lps.modules.app.entity.AppWork;
import com.winner.lps.modules.app.service.AppWorkService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 工种管理Controller
 *
 * @author 程少威
 * @version 2017-04-09
 */
@Controller
@RequestMapping(value = "${adminPath}/app/appWork")
public class AppWorkController extends BaseController {

    @Autowired
    private AppWorkService appWorkService;

    @ModelAttribute
    public AppWork get(@RequestParam(required = false) String id) {
        AppWork entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = appWorkService.get(id);
        }
        if (entity == null) {
            entity = new AppWork();
        }
        return entity;
    }

    @RequiresPermissions("app:appWork:view")
    @RequestMapping(value = {"list", ""})
    public String list(AppWork appWork, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<AppWork> page = appWorkService.findPage(new Page<AppWork>(request, response), appWork);
        model.addAttribute("page", page);
        return "modules/app/appWorkList";
    }

    @RequiresPermissions("app:appWork:view")
    @RequestMapping(value = "form")
    public String form(AppWork appWork, Model model) {
        model.addAttribute("appWork", appWork);
        return "modules/app/appWorkForm";
    }

    @RequiresPermissions("app:appWork:edit")
    @RequestMapping(value = "save")
    public String save(AppWork appWork, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, appWork)) {
            return form(appWork, model);
        }
        appWorkService.save(appWork);
        addMessage(redirectAttributes, "保存工种管理成功");
        return "redirect:" + Global.getAdminPath() + "/app/appWork/?repage";
    }

    @RequiresPermissions("app:appWork:edit")
    @RequestMapping(value = "delete")
    public String delete(AppWork appWork, RedirectAttributes redirectAttributes) {
        appWorkService.delete(appWork);
        addMessage(redirectAttributes, "删除工种管理成功");
        return "redirect:" + Global.getAdminPath() + "/app/appWork/?repage";
    }

}